package com.erp.module.xiSuZhiXiang

import androidx.appcompat.app.AlertDialog
import android.view.View
import android.view.inputmethod.EditorInfo
import com.erp.Myapp
import com.erp.R
import com.erp.bean.XiSuZhiXiangBean
import com.erp.mclass.QuanXian
import com.erp.myAdapter.ArrayAdapter
import com.erp.myAdapter.HongLvZhiXiangKAdapter
import com.erp.myFragment.BaseKFragment
import com.erp.util.Dialogutil
import com.erp.util.Myutil
import com.erp.view.PullToRefreshView
import kotlinx.android.synthetic.main.fragment_xisu_zhixiang.*
import java.util.*

/**
 * Created by Administrator on 2018/7/21/021.
 * 作者：刘洋
 * 吸塑纸箱界面
 */
class FragmentXiSuZhiXiang : BaseKFragment(), XiSuZhiXiangContract.XisuZhiXiangView, View.OnClickListener, PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener {
    companion object {
        val FENPEITIME="ZHIXIANGTIME"
    }
    private val f = "FragmentKZhiXiang"
    var updialog: AlertDialog?=null
    lateinit var P: IXiSuZhiXiangP
    lateinit var adapter: HongLvZhiXiangKAdapter
    lateinit var jiehaolist: MutableList<XiSuZhiXiangBean>
    var zhiliangid: MutableList<String> = ArrayList<String>()
    var zhiliangname: MutableList<String> = ArrayList()
    var wuliao = "0"
    override fun getLayoutId(): Int {
        fragmentname = f
        return R.layout.fragment_xisu_zhixiang
    }

    override fun initData() {
        jiehaolist = ArrayList<XiSuZhiXiangBean>()
        P = IXiSuZhiXiangP(this, activity!!,jiehaolist)
        SouSuoEt.hint = "输入节号搜索"
        adapter = HongLvZhiXiangKAdapter(jiehaolist, activity!!, P, QuanXian.isEditAllBoardCartonDistribution, QuanXian.isCancelBoardCartonDistribution)
        JiJianLv.adapter = adapter
        JiJianFragmentPv.setheader(true)
        JiJianFragmentPv.setOnHeaderRefreshListener(this)
        JiJianFragmentPv.setOnFooterRefreshListener (this)
        BtGuolv.setOnClickListener(this)
        TvChongzhi.setOnClickListener(this)
        TvStartTime.setOnClickListener(this)
        TvEndTime.setOnClickListener(this)
        P.getWuliaoLeibie()
        SouSuoEt.setOnEditorActionListener { _, actionId, _ ->
            var isOK = true
            when (actionId) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    P.souSuoZi(SouSuoEt.text.toString())
                    P.souSousuoOrder(SouSuoOrderEt.text.toString())
                    P.shijiansousuo(SpWuLiao.selectedItem.toString(),TvStartTime.text.toString(),TvEndTime.text.toString())
                    wuliaoid()
                    P.wuliaoLeiBieId(wuliao)
                    updialog = Myutil.showupdatedialog(activity, "正在搜索纸箱数据", false)
                    P.xiala()
                    Myutil.notjianpan(activity, TvChongzhi)
                }
                else -> isOK = false
            }
            isOK
        }
        SouSuoOrderEt.setOnEditorActionListener { v, actionId, event ->
            var isOK = true
            when (actionId) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    P.souSuoZi(SouSuoEt.text.toString())
                    P.souSousuoOrder(SouSuoOrderEt.text.toString())
                    P.shijiansousuo(SpWuLiao.selectedItem.toString(),TvStartTime.text.toString(),TvEndTime.text.toString())
                    wuliaoid()
                    P.wuliaoLeiBieId(wuliao)
                    updialog = Myutil.showupdatedialog(activity, "正在搜索纸箱数据", false)
                    P.xiala()
                    Myutil.notjianpan(activity, TvChongzhi)
                }
                else -> isOK = false
            }
            isOK
        }
        val listsp = ArrayList<String>()
        listsp.add("")
        listsp.add("上传时间")
        listsp.add("分配时间")
        val adapter = ArrayAdapter(activity, R.layout.lv_sp_tvitem, listsp)
        SpWuLiao.adapter = adapter
        SpWuLiao.setSelection(1)
        TvStartTime.text = Myutil.getjulidate(-1)
        TvEndTime.text = Myutil.getjulidate(0)
        P.gainData("XiSuZhiXiang")
        P.shijiansousuo(SpWuLiao.selectedItem.toString(),TvStartTime.text.toString(),TvEndTime.text.toString())
        JiJianFragmentPv.headerRefreshing()
    }
    fun qingkong(){
        if(isFirstLoad){
            return
        }
        JiJianFragmentPv.headerRefreshing()
    }

    fun wuliaoid() {
        if (zhiliangname.contains(AtWuliaoLeibie.text.toString())) {
            zhiliangname.indices
                    .filter { zhiliangname[it] == AtWuliaoLeibie.text.toString() }
                    .forEach { wuliao = zhiliangid[it] }

        } else {
            wuliao = ""
        }
    }

    override fun fenPeiJieHaoOk(data: String) {
        P.xiala()
        updialog?.dismiss()
    }

    override fun fenPeiJieHaoNo(erroe: String) {
        updialog?.dismiss()
        Myutil.MyTost(Myutil.decodeUnicode(erroe))
    }

    override fun fenpeijiehao() {
        updialog = Myutil.showupdatedialog(activity, "正在访问服务器", false)
    }

    override fun onClick(v: View?) {
        when (v) {
            BtGuolv -> {
                P.shijiansousuo(SpWuLiao.selectedItem.toString(),TvStartTime.text.toString(),TvEndTime.text.toString())
                P.souSuoZi(SouSuoEt.text.toString())
                P.souSousuoOrder(SouSuoOrderEt.text.toString())
                updialog = Myutil.showupdatedialog(activity, "正在搜索纸箱数据", false)
                wuliaoid()
                P.wuliaoLeiBieId(wuliao)
                P.xiala()
                Myutil.notjianpan(activity, TvChongzhi)
            }
            TvChongzhi -> {
                SpWuLiao.setSelection(0)
                TvStartTime.text=""
                TvEndTime.text=""
                SouSuoEt.setText("")
                AtWuliaoLeibie.setText("")
                SouSuoOrderEt.setText("")
                wuliao="0"
                P.shijiansousuo(SpWuLiao.selectedItem.toString(),TvStartTime.text.toString(),TvEndTime.text.toString())
                P.souSuoZi(SouSuoEt.text.toString())
                P.souSousuoOrder(SouSuoOrderEt.text.toString())
                updialog = Myutil.showupdatedialog(activity, "正在搜索纸箱数据", false)
                wuliaoid()
                P.wuliaoLeiBieId(wuliao)
                P.xiala()
                Myutil.notjianpan(activity, TvChongzhi)
            }
            TvStartTime -> {
                Dialogutil.showBottoPopupWindow(TvStartTime, "选择开始时间", TvStartTime.text.toString(), activity)
            }
            TvEndTime -> {
                Dialogutil.showBottoPopupWindow(TvEndTime, "选择开始时间", TvEndTime.text.toString(), activity)
            }
        }
    }

    override fun wuliaoLeibieOk(zhiliangid: MutableList<String>, zhiliangname: MutableList<String>) {
        this.zhiliangid = zhiliangid
        this.zhiliangname = zhiliangname
        val av = ArrayAdapter(activity, android.R.layout.simple_dropdown_item_1line, zhiliangname)
        AtWuliaoLeibie.setAdapter(av)
        AtWuliaoLeibie.setOnClickListener { AtWuliaoLeibie.showDropDown() }
    }

    override fun wuliaoLeibieNo() {
        Myutil.MyTost("获取物料类别失败")
    }
    override fun xialaOk() {
        updialog?.dismiss()
        JiJianFragmentPv?.onHeaderRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun xialaNo() {
        updialog?.dismiss()
        JiJianFragmentPv?.onHeaderRefreshComplete()
    }

    override fun shanglaOk() {
        updialog?.dismiss()
        JiJianFragmentPv?.onFooterRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun shanglaNo() {
        updialog?.dismiss()
        JiJianFragmentPv?.onFooterRefreshComplete()
    }
    override fun gainDataOk() {
        adapter.notifyDataSetChanged()
    }

    override fun onFooterRefresh(view: PullToRefreshView) {
        P.shangla()
    }
    override fun onHeaderRefresh(view: PullToRefreshView) {
        P.xiala()
    }
    override fun PutTime() {
        Myapp.editor.putLong(FENPEITIME, Date().time / 1000)
        Myapp.editor.commit()
    }
}